<?php
/**
 * 分布式唯一ID生成
 * 可靠的ID生成库类
 */
namespace GK\Exception;
class NumberException
{
    const EPOCH = 1479533469598;
    const max12bit = 4095;
    const max41bit = 1099511627775;
    static $machineId = null;
    public static function machineId($mId = 0) {
        self::$machineId = $mId;
    }
    //订单编号
    public static function OID(){
        return date('Ymdhis').self::NewID();
    }
    //授权码格式
    public static function Token(){
        return 'STOA'.date('Ymdhis').self::NewID();
    }
    //用户编号
    public static function UID(){
        return date('Ymhi').self::NewID();
    }
    //项目密码
    public static function OrdMd(){
        return substr(self::NewID(),-6);
    }
    //新建ID
    public static function NewID(){
        $time = floor(microtime(true) * 1000);
        $time -= self::EPOCH;
        $base = decbin(self::max41bit + $time);
        if(!self::$machineId) {
            $machineid = self::$machineId;
        }else{
            $machineid = str_pad(decbin(self::$machineId), 10, "0", STR_PAD_LEFT);
        }
        $random = str_pad(decbin(mt_rand(0, self::max12bit)), 12, "0", STR_PAD_LEFT);
        $base = $base.$machineid.$random;
        return bindec($base);
    }
    public static function timeFromParticle($particle) {
        return bindec(substr(decbin($particle),0,41)) - self::max41bit + self::EPOCH;
    }
}